home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / examples / cgi / cgitest.sml < prev    next >
Encoding:
Text File  |  1997-08-18  |  3.4 KB  |  107 lines  |  [TEXT/R*ch]

  1. (* mosml/examples/cgi/cgitest.sml
  2.    Retrieve and print all fields from a CGI request.
  3.  
  4.    Peter Sestoft, 1997-05-07
  5.  *)
  6.  
  7. open Mosmlcgi;
  8.  
  9. fun output_string(s) =          
  10.     TextIO.output(TextIO.stdOut,"Content-type: text/html\n\n" ^ s ^ "\n\n");
  11.  
  12. fun showOpt (info, value) = 
  13.     case value of 
  14.     NONE   => print (info ^ " is not available\n<p>")
  15.       | SOME s => app print [info, " = ", s, "\n<p>"]
  16.  
  17. fun prtseq (pr, sep) xs = 
  18.     let fun h []      = ()
  19.       | h [x]     = pr x
  20.       | h (x::xr) = (pr x; print sep; h xr)
  21.     in h xs end
  22.  
  23. fun showList (info, value) = 
  24.     (print info; print " = "; 
  25.      prtseq (fn s => print s, ",") value; 
  26.      print "\n<p>")
  27.  
  28. fun adminfo () = 
  29.     (showList ("cgi_fieldnames", cgi_fieldnames);
  30.      showList ("cgi_partnames",  cgi_partnames);
  31.      app showOpt 
  32.      [("cgi_server_software",   cgi_server_software),
  33.       ("cgi_server_name",       cgi_server_name),
  34.       ("cgi_gateway_interface", cgi_gateway_interface),
  35.       ("cgi_server_protocol",   cgi_server_protocol),
  36.       ("cgi_server_port",       cgi_server_port),
  37.       ("cgi_request_method",    cgi_request_method),
  38.       ("cgi_http_accept",       cgi_http_accept),
  39.       ("cgi_http_user_agent",   cgi_http_user_agent),
  40.       ("cgi_http_referer",      cgi_http_referer),
  41.       ("cgi_path_info",         cgi_path_info),
  42.       ("cgi_path_translated",   cgi_path_translated),
  43.       ("cgi_script_name",       cgi_script_name),
  44.       ("cgi_query_string",      cgi_query_string),
  45.       ("cgi_remote_host",       cgi_remote_host),
  46.       ("cgi_remote_addr",       cgi_remote_addr),
  47.       ("cgi_remote_user",       cgi_remote_user),
  48.       ("cgi_remote_ident",      cgi_remote_ident),
  49.       ("cgi_auth_type",         cgi_auth_type),
  50.       ("cgi_content_type",      cgi_content_type),
  51.       ("cgi_content_length",    cgi_content_length),
  52.       ("cgi_annotation_server", cgi_annotation_server)])
  53.     
  54. fun showpart name =
  55.     let val part = valOf(cgi_part name)
  56.     in
  57.     print ("<H2>Part `" ^ name ^ "'</H2>\n");
  58.     app (fn field => showOpt (field, part_field_string part field))
  59.         (part_fieldnames part);
  60.     showOpt("Content type", part_type part);
  61.     print "<P><PRE>"; print (part_data part); print "</PRE>"
  62.     end
  63.  
  64. fun showfield field = showOpt (field, cgi_field_string field)
  65.  
  66. fun savepart name =
  67.     let val part = valOf (cgi_part name)
  68.     in 
  69.     case part_field_string part "filename" of
  70.         NONE          => ()
  71.       | SOME filename => 
  72.         let open BinIO
  73.             val tmpfile = "/tmp/" ^ filename
  74.             val os = openOut tmpfile
  75.         in 
  76.             output (os, Byte.stringToBytes (part_data part));
  77.             closeOut os;
  78.             print "<p>Saving file ";
  79.             print tmpfile;
  80.             print " (";
  81.             print (Int.toString (size (part_data part)));
  82.             print " bytes)"
  83.         end
  84.     end
  85.  
  86. fun err s = TextIO.output(TextIO.stdErr, s);
  87.  
  88. val _ = 
  89.     (print "Content-type: text/html\n\n";
  90.      print "<HTML><P><BR CLEAR>";
  91.      (case cgi_partnames of
  92.       [] => ()
  93.     | _  => (print "<H1>Parts and their contents</H1>";
  94.          app showpart (* savepart *) cgi_partnames));
  95.      (case cgi_fieldnames of
  96.       [] => ()
  97.     | _  => (print "<H1>CGI field names and their values</H1>";
  98.          app showfield cgi_fieldnames));
  99.      print "<H1>Administrative information</h1>";
  100.      adminfo ();
  101.      print "<P><HR>This text was generated by the mosmlcgi \
  102.       \script testcgi.sml on ";
  103.      print (Date.toString(Date.fromTime(Time.now())));
  104.      print "</BODY></HTML>")
  105.     handle exn => err "An exception occurred"
  106.  
  107.